import { Constant } from "../constant/Constant";

// Learn TypeScript:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class DogerFixed extends cc.Component {

    @property(cc.Node)
    middleDog: cc.Node = null;


    @property(cc.Node)
    addScore: cc.Node = null;

    @property(cc.Node)
    leftObj: cc.Node = null;
    @property(cc.Node)
    rightObj: cc.Node = null;


    // onLoad () {}
    private autoFixed(dogoer: cc.Node) {
        let totalW = this.leftObj.width + this.rightObj.width;
        let screenW = Constant.SCREEN.width;
        let length = dogoer.width = screenW - totalW;
        dogoer.getComponent(cc.BoxCollider).size.width = dogoer.width;
        dogoer.height = dogoer.getComponent(cc.BoxCollider).size.height;
        dogoer.x = 0;
    }

    onLoad() {
    }

    start() {
        this.middleDog = cc.find("bigDog", this.node);
        this.node.y = Constant.SCREEN.height / 2;
        this.autoFixed(this.middleDog);
        this.addScore.getComponent(cc.BoxCollider).size.width = Constant.SCREEN.width;
    }

    update(dt) {
        this.movingPlaneBySpeed(dt);
    }

    private movingPlaneBySpeed(dt) {
        let t = this;
        let movingNode: cc.Node = t.node;
        if (movingNode.y < -Constant.SCREEN.height) {
            movingNode.y = Constant.SCREEN.height / 2;
            movingNode.active = false;
        } else {
            let action = cc.moveBy(dt, new cc.Vec2(0, - Constant.GAME_SPEED / 2))
            movingNode.runAction(action);
        }
    }
}
